package com.turing.post.report.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.account.util.OnlineTransferDetail;
import com.turing.post.account.util.OnlineTransferDetailDao;
import com.turing.post.report.util.OnlineTransferChkAccCtl;
import com.turing.post.report.util.OnlineTransferChkAccCtlBusiness;
import com.turing.post.report.util.OnlineTransferChkAccCtlDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "account.impl.OnlineTransferChkAccCtlBusinessImpl")
@Remote
@Local
public class OnlineTransferChkAccCtlBusinessImpl implements
		OnlineTransferChkAccCtlBusiness {

	@EJB(mappedName = "account.impl.OnlineTransferDetailDaoImpl")
	private OnlineTransferDetailDao onlineTransferDetailDao;

	@EJB(mappedName = "account.impl.OnlineTransferChkAccCtlDaoImpl")
	private OnlineTransferChkAccCtlDao onlineTransferChkAccCtlDao;

	@Override
	public void insertOnlineTransferChkAccCtl(String issueCompanyNo,
			Date tranDate) throws PCPSApplicationException {
		//联机转账总汇
		List<Object[]> list = onlineTransferDetailDao
				.generateOnlineTransferDetail(issueCompanyNo, tranDate);
		OnlineTransferChkAccCtl otcc = null;
		//判断是否有联机转账总汇信息
		if (list != null && list.size() > 0) {
			//循环联机转账总汇,插入或者更新联机转账对帐控制信息
			for (Object[] obj : list) {
				otcc = new OnlineTransferChkAccCtl();
				otcc.setBankManageId(new BigInteger(obj[0].toString()));
				otcc.setCheckDate(tranDate);
				OnlineTransferChkAccCtl onlineTransferChkAccCtl = onlineTransferChkAccCtlDao
						.getOnlineTransferChkAccCtl(otcc);
				//判断是否已有联机转账对帐控制信息,为空则插入
				if (onlineTransferChkAccCtl == null){
					otcc.setIssueCompanyNo(obj[2].toString());
					String TranType  = obj[3].toString();
					//判断联机转账总汇信息为借方还是贷方
					if (OnlineTransferDetail.OnlineTransfer_TranType_deposit.equals(TranType)){
						otcc.setDebitCount(Integer.parseInt(obj[4].toString()));
						otcc.setDebitAmt(new BigInteger(obj[5].toString()));
						otcc.setCreditCount(0);
						otcc.setCreditAmt(BigInteger.ZERO);
					}else{
						otcc.setDebitCount(0);
						otcc.setDebitAmt(BigInteger.ZERO);
						otcc.setCreditCount(Integer.parseInt(obj[4].toString()));
						otcc.setCreditAmt(new BigInteger(obj[5].toString()));
					}
					otcc.setCheckResult(OnlineTransferChkAccCtl.ONLINEACC_NOTACC);
					onlineTransferChkAccCtlDao.insert(otcc);
				//判断是否已有联机转账对帐控制信息,不为空则更新
				}else{
					String TranType  = obj[3].toString();
					//判断联机转账总汇信息为借方还是贷方
					if (OnlineTransferDetail.OnlineTransfer_TranType_deposit.equals(TranType)){
						onlineTransferChkAccCtl.setDebitCount(Integer.parseInt(obj[4].toString()));
						onlineTransferChkAccCtl.setDebitAmt(new BigInteger(obj[5].toString()));
					}else{
						onlineTransferChkAccCtl.setCreditCount(Integer.parseInt(obj[4].toString()));
						onlineTransferChkAccCtl.setCreditAmt(new BigInteger(obj[5].toString()));
					}
					onlineTransferChkAccCtlDao.update(onlineTransferChkAccCtl);
				}
				 
			}
		}
	}
}
